package L620;

import java.util.Arrays;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-20 17:52
 * @version: 1.0
 */
public class FindStartAndEnd {
    public int[] searchRange(int[] nums, int target) {
        int i = binarySearch(nums, target);
        int i1 = binarySearch2(nums, target);
        return new int[]{i, i1};
    }

    public int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
//        right=mid；找最位置元素
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
    public int binarySearch2(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
//        找最右元素，mid = (left + right+1) / 2;
        while (left < right) {
            int mid = (left + right+1) / 2;
            if (nums[mid] > target) {
                right = mid-1;
            } else {
                left = mid ;
            }
        }
        return right;
    }

    public static void main(String[] args) {
        int[] ints = {5, 7, 7, 8, 8, 10};
//        int i = new FindStartAndEnd().binarySearch(ints, 4);
        int[] ints1 = new FindStartAndEnd().searchRange(ints, 8);

        System.out.println(Arrays.toString(ints1));
    }
}
